<template>
  <div class="quiz-container">
    <!-- 顶部进度条 -->
    <van-progress
      :percentage="(currentQuestionIndex / questions.length) * 100"
      :show-pivot="false"
      color="#1989fa"
    />

    <!-- 题目序号 -->
    <div class="question-counter">
      第 {{ currentQuestionIndex + 1 }} 题 / 共 {{ questions.length }} 题
    </div>

    <!-- 题目内容 -->
    <div class="question-content">
      <h2>{{ currentQuestion.text }}</h2>
    </div>

    <!-- 选项列表 -->
    <van-radio-group v-model="answers[currentQuestionIndex]">
      <van-cell-group>
        <van-cell
          v-for="option in currentQuestion.options"
          :key="option.value"
          clickable
          @click="selectOption(option.value)"
        >
          <template #title>
            <span class="option-label">{{ option.label }}.</span>
            <span class="option-text">{{ option.text }}</span>
          </template>
          <template #right-icon>
            <van-radio :name="option.value" />
          </template>
        </van-cell>
      </van-cell-group>
    </van-radio-group>

    <!-- 导航按钮 -->
    <div class="navigation-buttons">
      <van-button
        round
        type="primary"
        plain
        :disabled="currentQuestionIndex === 0"
        @click="prevQuestion"
      >
        上一题
      </van-button>
      <van-button
        v-if="currentQuestionIndex < questions.length - 1"
        round
        type="primary"
        @click="nextQuestion"
      >
        下一题
      </van-button>
      <van-button
        v-else
        round
        type="success"
        @click="submitQuiz"
      >
        提交答案
      </van-button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { showDialog } from 'vant';

// 模拟20道题目数据
const generateQuestions = () => {
  const questions = [];
  for (let i = 1; i <= 2; i++) {
    questions.push({
      id: i,
      text: `这是第 ${i} 道题目的内容，请选择正确的答案？`,
      options: [
        { label: 'A', value: 'A', text: '选项A的内容' },
        { label: 'B', value: 'B', text: '选项B的内容' },
        { label: 'C', value: 'C', text: '选项C的内容' },
        { label: 'D', value: 'D', text: '选项D的内容' }
      ],
      correctAnswer: ['A', 'B', 'C', 'D'][Math.floor(Math.random() * 4)] // 随机正确答案
    });
  }
  return questions;
};

const questions = ref(generateQuestions());
const currentQuestionIndex = ref(0);
const answers = ref(Array(2).fill(null)); // 初始化答案数组

const currentQuestion = computed(() => questions.value[currentQuestionIndex.value]);

const selectOption = (value) => {
  answers.value[currentQuestionIndex.value] = value;
};

const nextQuestion = () => {
  if (currentQuestionIndex.value < questions.value.length - 1) {
    currentQuestionIndex.value++;
  }
};

const prevQuestion = () => {
  if (currentQuestionIndex.value > 0) {
    currentQuestionIndex.value--;
  }
};

const submitQuiz = () => {
  // 检查是否所有题目都已回答
  const unanswered = answers.value.filter(answer => answer === null).length;
  if (unanswered > 0) {
    showDialog({
      title: '提示',
      message: `您还有 ${unanswered} 道题目未回答，确定要提交吗？`,
    }).then(() => {
      calculateScore();
    });
  } else {
    calculateScore();
  }
};

const calculateScore = () => {
  let score = 0;
  questions.value.forEach((question, index) => {
    if (answers.value[index] === question.correctAnswer) {
      score++;
    }
  });

  showDialog({
    title: '答题结果',
    message: `您答对了 ${score} 题，得分 ${(score / questions.value.length * 100).toFixed(1)} 分`,
    confirmButtonText: '查看详情',
    cancelButtonText: '关闭'
  }).then(() => {
    // 跳转到结果详情页
    // router.push('/quiz-result');
  });
};
</script>

<style scoped>
.quiz-container {
  padding: 16px;
  max-width: 600px;
  margin: 0 auto;
}

.question-counter {
  text-align: center;
  margin: 12px 0;
  color: #666;
  font-size: 14px;
}

.question-content {
  margin: 20px 0;
  padding: 0 8px;
}

.option-label {
  font-weight: bold;
  margin-right: 8px;
}

.navigation-buttons {
  display: flex;
  justify-content: space-between;
  margin-top: 24px;
  padding: 0 16px;
}

.van-progress {
  margin-bottom: 12px;
}
</style>
